package cn.com.bob.library.controller;

import cn.com.bob.library.entity.Book;
import cn.com.bob.library.service.BookService;
import cn.com.bob.library.service.ex.BookNameModifyException;
import cn.com.bob.library.service.ex.ServiceException;
import cn.com.bob.library.util.JsonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Service;
import java.awt.print.Pageable;
import java.util.List;

/**
 * @Author Bob
 * @Version：1.0
 */
@RestController
@RequestMapping("/book")
public class BookController extends BaseController{
    @Autowired
    private BookService bookService;

    /**
     * 获取所有图书
     * @param :
     * @return JsonResult<Void>
     */
    @GetMapping("getAllBooks")
    public List<Book> getAllBooks(){
        return bookService.queryAllBooks();
    }

    /**
     * 获取可借阅图书
     * @param :
     * @return JsonResult<Void>
     */
    @GetMapping("getAvailableBooks")
    public List<Book> getAvailableBooks(){
        return bookService.queryAvailableBooks();
    }
    /**
     * 获取可借阅图书
     * @param :
     * @return JsonResult<Void>
     */
    @GetMapping("getUnavailableBooks")
    public List<Book> getUnavailableBooks(){
        return bookService.queryUnavailableBooks();
    }
    /**
     * 新增图书
     * @param book:
     * @return JsonResult<Book>
     */
    @PostMapping("/addBook")
    public JsonResult<Book> addBook(@RequestBody Book book,HttpSession session){
        bookService.addBook(book);
        return new JsonResult<>(OK,book);
    }

    @PutMapping("/updateBook")
    public JsonResult<Void> updateBook(@RequestBody Book book){
        JsonResult<Void> result = new JsonResult<>(OK);

        bookService.updateMsg(book);
        return result;
    }

    @DeleteMapping("/deleteBook")
    public JsonResult<Void> deleteBook(@RequestBody Book book){
        bookService.deleteBook(book);
        return new JsonResult<>(OK);
    }

    /**
     * 根据书名进行查询，可实现模糊查询
     * @param title:
     * @return List<Book>
     */
    @GetMapping("/getBookByTitle")
    public  List<Book> getBookByTitle(String title){
        return bookService.getBookByTitle(title);
    }

    /**
     * 借书操作
     * @param book:
     * @param session:
     * @return JsonResult<Void>
     */
    @PostMapping("/borrowBook")
    public JsonResult<Void> borrowBook(@RequestBody Book book, HttpSession session){
        bookService.borrowBook(book,getUsernameFromSession(session));
        return new JsonResult<>(OK);
    }

    @GetMapping("/getBooksByPage")
    public List<Book> getBooksByPage(@RequestParam("page") int pageNo,@RequestParam("limit") int pageSize){
        IPage<Book> res = bookService.getBooksByPage(pageNo, pageSize);
        System.out.println( "pages:" + res.getPages());
        System.out.println( "current:" + res.getCurrent());
        System.out.println( "size:" + res.getSize());
        System.out.println( "total:" + res.getTotal());
        res.getRecords().forEach(System.out::println);
        return res.getRecords();

    }
}
